﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace BusinessLogic
{
    public class WXUserLogic
    {
        //public ObjectResult<UserInfo> GetPageList(int pagenum, int? seOrderStatus, string OrderID, string ShopName, string StyleName, string UserTel, string StartTime, string EndTime, ref int totalCount)
        //{
        //    //Model1Container db = new Model1Container();
        //    //ObjectParameter totalCountParameter = new ObjectParameter("TotalCount", 0);

        //    //ObjectResult<UserInfo> result = db.GetOrderlistAdmin(pagenum, seOrderStatus, OrderID, ShopName, StyleName, UserTel, StartTime, EndTime, totalCountParameter);
        //    //totalCount = Convert.ToInt32(totalCountParameter.Value);
        //    //return result;
        //}
        public List<dynamic> getPageDate<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int Total)
           where T : class
        {
            Model1Container db = new Model1Container();
            Total = db.Set<T>().Where(where).Count();
            var list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }
        public int UnLock(string id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.UserInfo.First(p => p.ID == id);
                    model.Status = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int Lock(string id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.UserInfo.First(p => p.ID == id);
                    model.Status = 2;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int DelInfo(string id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.UserInfo.First(p => p.ID == id);
                    model.IsDelete = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int SetToStyle(string id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.UserInfo.First(p => p.ID == id);
                    if (string.IsNullOrEmpty(model.Tel))
                    {
                        return 5;//该微信手机号为空，无法设置为发型师
                    }
                    var style = db.Stylist.Where(x => x.Tel == model.Tel && x.Isdelete == 0).ToList();
                    if (style == null||style.Count()==0)
                    {
                        return 2;//不存在 请先在系统中添加该发型师
                    }
                    else  if (style.Count()>1)
                    {
                        return 3;//系统中存在多个发型师的电话号码相同。请检查
                    }
                    else
                    {
                        //using (var scope = db.Database.BeginTransaction())
                        //{
                        //    try
                        //    {
                        //        UserInfo u_model = new UserInfo();
                        //        u_model.UserType = 2;
                        //        db.SaveChanges();
                        //    }
                        //    catch (Exception ex)
                        //    {

                        //    }
                        //}

                        model.UserType = 2;
                        db.SaveChanges();
                        return 1;
                    }
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int SetStyleToUser(string id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.UserInfo.First(p => p.ID == id);
                    var style = db.Stylist.Where(x => x.Tel == model.Tel&&x.Isdelete==0);
                    if (style != null && style.Count()>0)
                    {
                        return 2;// 请先删除系统中的发型师
                    }
                    else
                    {
                        UserInfo u_model = new UserInfo();
                        u_model.UserType = 1;
                        db.SaveChanges();
                        return 1;
                    }
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        
    }
}
